package com.ruoyi.system.api.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteDictService;

/**
 * 字典服务降级处理
 * 
 * @author ruoyi
 */
@Component
public class RemoteDictFallbackFactory implements FallbackFactory<RemoteDictService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteDictFallbackFactory.class);

    @Override
    public RemoteDictService create(Throwable throwable)
    {
        log.error("字典服务调用失败：{}", throwable.getMessage());
        return new RemoteDictService()
        {
            @Override
            public R<String> getDictLabel(String dictType, String dictValue)
            {
                // 如果远程调用失败，返回字典值本身作为降级处理
                log.warn("字典服务降级处理：dictType={}, dictValue={}", dictType, dictValue);
                // 确保即使dictValue为null，也返回一个非空的默认值
                String result = (dictValue != null) ? dictValue : "";
                return R.ok(result);
            }
        };
    }
}